package tools.cyan.com.myapplication.dagger;

import android.provider.SyncStateContract;
import android.text.TextUtils;

import com.cyan.tools.util.HttpLogger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import tools.cyan.com.myapplication.app.App;
import tools.cyan.com.myapplication.bean.ApiService;
import tools.cyan.com.myapplication.config.Constants;
import tools.cyan.com.myapplication.module.contract.TestContract;
import tools.cyan.com.myapplication.module.model.TestModelImpl;

/**
 * Created by pc on 2018/4/25.
 */
@Module
public class TestModules {
    @Singleton
    @Provides
    protected ApiService getService(ScalarsConverterFactory scalarsConverterFactory, RxJava2CallAdapterFactory callAdapterFactory, GsonConverterFactory gsonConverterFactory, OkHttpClient client) {
        return new Retrofit.Builder().baseUrl(Constants.BASE_URL)
                .addConverterFactory(scalarsConverterFactory)
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(callAdapterFactory)
                .client(client)
                .build().create(ApiService.class);
    }

    @Singleton
    @Provides
    protected ScalarsConverterFactory getScalarsConverterFactory() {
        return ScalarsConverterFactory.create();
    }

    @Singleton
    @Provides
    protected RxJava2CallAdapterFactory getCallAdapterFactory() {
        return RxJava2CallAdapterFactory.create();
    }

    @Singleton
    @Provides
    protected GsonConverterFactory getGsonConvertFactory() {
        return GsonConverterFactory.create();
    }

    @Singleton
    @Provides
    protected OkHttpClient getClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLogger());
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .addNetworkInterceptor(logging).retryOnConnectionFailure(true)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        if(chain.request().url().toString().contains("http://up.qiniu.com"))return chain.proceed(chain.request());
                        return TextUtils.isEmpty(App.getApp().getToken()) ?chain.proceed(chain.request()) : chain.proceed(chain.request().newBuilder().addHeader("Authorization", App.getApp().getToken()).build());
                    }
                })
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(20000, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
    }
}
